var canshu=1;
var pingtai='0'
var CangName=""
var code=""
var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

$.each(warehouses.data,function(k,s){
    code +=s.warehouseCode+ ","
    CangName+=s.warehouseName+"/"

})
//去掉最后一个逗号(如果不需要去掉，就不用写)
if (code.length > 0) {
    code = code.substr(0, code.length - 1);
}
if (CangName.length > 0) {
    CangName = CangName.substr(0, CangName.length - 1);
}
// $("#putong").attr("checked", true);
// $("#qianyue").attr("checked", true);
// 保存按钮
$("#form-member").validate({
    rules: {
        title:"titleCheck"
    },
    //提交表单数据
    submitHandler:function(form){
        //循环class，获取到每个check的选中状态
        if (pingtai=='0'||code=='0'){
            if (pingtai=='0'){
                $("#putong").css('outline','1px solid red')
                $("#qianyue").css('outline','1px solid red')
            }
            if (code==''){
                var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

                $.each(warehouses.data,function(k,s){
                    var warecode= document.getElementById('yun'+s.warehouseCode+'')
                    $(warecode).css('outline','1px solid red')

                })
            }
            layer.msg("请选择配置平台/地域？", {icon: 6, time: 3000});
            return false
        }
        //获取
        var title=$("#title").val(),
            url = $("#url").val(),
            started_at = $("#logmin").val(),
            end_in = $("#logmax").val(),
            category_id = getQueryString("category_id"),
            p=pingtai,
            wa=code,
            waname=CangName
        //提交
        var quzhi=JSON.parse(web.readCookie("loginData"))
        var user_data = {
            keyword : title,
            image_url : url,
            started_at : started_at,
            end_in : end_in,
            category_id :category_id,
            created_name:quzhi.user.username,
            created_by:quzhi.user.user_ID,
            /*平台和仓库*/
            platform:p,
            warehouse_code:wa,
            warehouse_name:waname,
            ordinary_image_url:$("#ordinaryUrl").val()
        };
        //获取返回的数据
        var succCallBack = function (data, status, response) {
            console.log(data);
            if (data.errorCode=="92"){
                canshu=1
                layer.msg('您添加的开始时间小于已存在数据的结束时间!', {icon: 6, time: 3000});
                return
            }
            if (data.errorCode=="94"){
                canshu=1
                layer.msg(data.errorMessage, {icon: 6, time: 3000});
                return
            }
            if (data.errorCode=="0"){
                layer.msg('操作成功!', {icon: 6, time: 1000});
                parent.queryDataFn();
                setTimeout( function () {
                    cancelLayerNoRefreshFn();
                },700);
            }
            if (data.errorCode=="93"){
                canshu=1
                layer.msg('开始时间不能大于结束时间,请重新查看', {icon: 6, time: 3000});
                return
            }
        }
        //给一个接口地址提交数据
        canshu=canshu+1;
        if (canshu>2){
            return false;
        }else{
            return web.doAjax(root.sroreinter.save_resource_conf, 'post', user_data, succCallBack);
        }
    }
})
function changePingtai() {
    $("#putong").css('outline','')
    $("#qianyue").css('outline','')

    if ($("#qianyue").is(":checked")&&$("#putong").is(":checked")){
        $("#url").css('display','inline');
        $("#ordinaryUrl").css('display','inline');
        pingtai='10'+","+'20'
    }
    if ($("#qianyue").is(":checked")&&!$("#putong").is(":checked")){
        $("#url").css('display','inline');
        $("#ordinaryUrl").css('display','none');
        pingtai='10'
    }
    if ($("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#url").css('display','none');
        $("#ordinaryUrl").css('display','inline');
        pingtai='20'
    }
    if (!$("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#url").css('display','none');
        $("#ordinaryUrl").css('display','none');
        pingtai='0'
    }
}
function changeCang() {
    code=''
    CangName=''
    var warcode=""//id
    var warcodename=""//name
    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data,function(k,s){
        if(document.getElementById('yun'+s.warehouseCode+'').checked==true){//获取选中的id
            warcode+=document.getElementById('yun'+s.warehouseCode+'').value+ ","//逗号拼接获取到的所以id
            warcodename+=document.getElementById('yun'+s.warehouseCode+'').name+ "/"
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    if (warcodename.length > 0) {
        warcodename = warcodename.substr(0, warcodename.length - 1);
    }

    code=warcode
    CangName=warcodename
}
//初始化客户归属下拉列表
function ascription() {
    var user_data = {}
    var succCallBack = function (data, status, response) {
        if (data.errorCode == "0") {
            var dataInter = data.group;
            var interText = doT.template($("#ascription_bob").text());
            $("#ascription_lsit").html(interText(dataInter));
        }
    };
    return web.doAjax(root.inter.getGroup, 'post', user_data, succCallBack);
}
function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}